import { createRouter, createWebHistory } from 'vue-router'
import { getToken } from '../utils/auth'

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/admin',
        name: 'Admin',
        component: () => import('../views/Admin.vue'),
        meta: { requiresAuth: true },
        children: [
            // 飞机群管理
            {
                path: 'groups',
                name: 'Groups',
                component: () => import('../views/admin/Groups.vue')
            },
            {
                path: 'members',
                name: 'Members',
                component: () => import('../views/admin/Members.vue')
            },
            // 积分管理
            {
                path: 'points',
                name: 'Points',
                component: () => import('../views/admin/points/Index.vue'),
                children: [
                    {
                        path: 'rules',
                        name: 'PointsRules',
                        component: () => import('../views/admin/points/Rules.vue')
                    },
                    {
                        path: 'lottery',
                        name: 'PointsLottery',
                        component: () => import('../views/admin/points/Lottery.vue')
                    },
                    {
                        path: 'lottery/create',
                        name: 'CreateLottery',
                        component: () => import('../views/admin/points/CreateLottery.vue')
                    },
                    {
                        path: 'lottery/:id',
                        name: 'LotteryDetail',
                        component: () => import('../views/admin/points/LotteryDetail.vue'),
                        meta: { requiresAuth: true }
                    },
                    {
                        path: 'records',
                        name: 'PointsRecords',
                        component: () => import('../views/admin/points/Records.vue'),
                        meta: { title: '抽奖记录' }
                    },
                    {
                        path: 'logs',
                        name: 'PointLogs',
                        component: () => import('../views/admin/points/Logs.vue')
                    },
                    {
                        path: 'ranking',
                        name: 'PointsRanking',
                        component: () => import('../views/admin/points/Ranking.vue')
                    }
                ]
            },
            // 群控管理
            {
                path: 'group-control',
                name: 'GroupControl',
                component: () => import('../views/admin/group-control/Index.vue'),
                children: [
                    {
                        path: 'overview',
                        name: 'GroupControlOverview',
                        component: () => import('../views/admin/group-control/Overview.vue'),
                        meta: { title: '群控概览' }
                    },
                    {
                        path: 'messages',
                        name: 'MessageManagement',
                        component: () => import('../views/admin/group-control/Messages.vue'),
                        meta: { title: '消息管理' }
                    },
                    {
                        path: 'filter',
                        name: 'SensitiveFilter',
                        component: () => import('../views/admin/group-control/Filter.vue'),
                        meta: { title: '敏感词过滤' }
                    },
                    {
                        path: 'verification',
                        name: 'EntryVerification',
                        component: () => import('../views/admin/group-control/Verification.vue'),
                        meta: { title: '入群验证' }
                    }
                ]
            },
            // 帮助指南
            {
                path: 'help',
                name: 'Help',
                component: () => import('../views/admin/help/Index.vue'),
                meta: { title: '帮助中心' },
                children: [
                    {
                        path: 'forward-guide',
                        name: 'ForwardGuide',
                        component: () => import('../views/admin/help/ForwardGuide.vue'),
                        meta: { title: '转发功能指南' }
                    }
                ]
            }
        ]
    },
    {
        path: '/',
        redirect: '/admin/groups'
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requiresAuth)) {
        if (!getToken()) {
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            })
        } else {
            next()
        }
    } else {
        next()
    }
})

export default router 